package cn.xdf.dao;

import cn.xdf.entity.User;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserDao extends JpaRepository<User,Integer>, JpaSpecificationExecutor<User> {

    //方法命名查询


    @Query("from User")
    List<User> findAll_JPQL();


    @Query("from User u where u.id=?1")//?1表示取第一个参数
    User findById_JPQL(Integer id);

    @Query("update User u set u.username=?1 where u.id=?2")
    @Modifying
    void update_JPQL(String username,Integer id);




    @Query(value = "select * from user",nativeQuery = true)
    List<User> findAll_SQL();


    @Query(value = "select * from user u where u.id=?1",nativeQuery = true)//?1表示取第一个参数
    User findById_SQL(Integer id);

    @Query(value = "update user u set u.username=?1 where u.id=?2",nativeQuery = true)
    @Modifying
    void update_SQL(String username,Integer id);

    //通过用户名精确查询
    User findByUsername(String username);

    //通过用户名模糊查询
    List<User> findByUsernameLike(String username);

    //通过用户名模糊查询最后按生日降序
    List<User> findByUsernameLikeOrderByBirthdayDesc(String username);


}
